package org.example.cake.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.lettuce.core.dynamic.annotation.Param;
import org.example.cake.common.Rep;
import org.example.cake.common.Repsecond;
import org.example.cake.entity.Cake;
import org.example.cake.entity.User;
import org.example.cake.service.CakeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/cake")
public class CakeController {
    @Autowired
    private CakeService cakeService;
    /**
     * 根据分类id查询蛋糕集合
     * @param categoryid 分类id
     * @return 蛋糕集合
     */
    @GetMapping("/getCakeListByCategoryId")
    public Rep getCakeListByCategoryId(Integer categoryid){
        List<Cake> cakeListByCategoryId = this.cakeService.getCakeListByCategoryId(categoryid);
        return new Rep().addData("cakeListByCategoryId",cakeListByCategoryId);
    }
    /**
     * 根据蛋糕id查询蛋糕信息
     * @param cakeid 蛋糕id
     * @return 蛋糕信息
     */
    @GetMapping("/getCakeById")
    public Rep getCakeById(Integer cakeid){
        Cake cakeById = this.cakeService.getCakeById(cakeid);
        return new Rep().addData("cakeById",cakeById);
    }
    /**
     * 查询所有蛋糕
     *
     * @return 所有蛋糕
     */
    @GetMapping("/getAllCake")
    public Rep getAllCake(@RequestParam(required = false) String cakename,
                          @RequestParam(required = false) Integer status,
                          Integer page, Integer limit) {

        page = page == null ? 1 : page;
        limit = limit == null ? 10 : limit;
        PageHelper.startPage(page, limit);
        List<Cake> allCake = this.cakeService.getCakeList(cakename, status);
        PageInfo<Cake> pageInfo = new PageInfo<>(allCake);
        return new Rep().addData("allCake", pageInfo);
    }
    /**
     * 模糊查询蛋糕
     * @Date 2025-07-28 谭丽娜
     * @param searchval 蛋糕
     * @return
     */
    @PostMapping("/selectCakeList")
    public Rep selectCakeList(@RequestBody String searchval){
        System.out.println("searchval = " + searchval);
       //模糊查询输入古来的字符串
        List<Cake> cakeList=cakeService.searchCake(searchval);
        return new Rep().addData("cakeList",cakeList);
    }/**
     * 查询新品蛋糕
     * @return 蛋糕集合
     */
    @GetMapping("/selectNewCakeList")
    public Rep selectNewCakeList(){
        List<Cake> newCakeList = cakeService.selectNewCakeList();
        return new Rep().addData("newCakeList",newCakeList);
    }
    /**
     * 查询精选蛋糕
     * @return 蛋糕集合
     */
    @GetMapping("/selectChoiceCakeList")
    public Rep selectChoiceCakeList(){
        List<Cake> choiceCakeList = cakeService.selectChoiceCakeList();
        return new Rep().addData("choiceCakeList",choiceCakeList);
    }
    /**
     * 根据蛋糕cakeId修改蛋糕状态
     *
     * @param map
     * @return
     */
    @PostMapping("/updateCakeHou")
    public Rep updateCakeHou(@RequestBody Map<String, Object> map) {
        // 1. 校验参数
        System.out.println("map = " + map);
        if (!map.containsKey("cakeId") || !(map.get("cakeId") instanceof List))

            return new Rep().addData("msg", "参数错误");

        // 2. 取出用户 ID 列表
        List<Long> ids = (List<Long>) map.get("cakeId");

        System.out.println("ids = " + ids);
        // 3. 调用业务层批量切换状态
        return new Rep().addData("update", cakeService.updateCakeHou(ids));
    }

    @PostMapping("/updateCakeInfo")
    public Rep getCakeList(@RequestBody Cake cake) {
        return new Rep().addData("cakeList", cakeService.updateCakeInfo(cake));
    }
    @PostMapping("/insertCakeInfo")
    public Rep insertCakeInfo(@RequestBody Cake cake) {
        return new Rep().addData("cakeList", cakeService.insert(cake));
    }

    /**
     * 判断蛋糕是否上架
     * @param cakeid
     * @return
     */
    @PostMapping("/ifcakeStatusapi")
    public Rep ifcakeStatusapi(@RequestBody Integer cakeid) {
        Cake cake=cakeService.getCakeById(cakeid);
        return new Rep().addData("cake",cake);
    }
}
